package com.lebang.controller.api;


import com.lebang.common.Login;
import com.lebang.component.AsyncsComponent;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.model.WeixinInfo;
import com.lebang.model.enums.Gender;
import com.lebang.service.WxPayService;
import com.lebang.service.buss.MemberService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author: hwq
 * @Date: 2019/9/6 15:18
 * @Version 1.0
 */
@Controller
@RequestMapping("/weixin")
public class WxApi {


    @Autowired
    private AsyncsComponent asyncsComponent;
    @Autowired
    private MemberService memberService;
    @Autowired
    private WxPayService wxPayService;

    @ResponseBody
    @GetMapping("/notifly")
    public String notifly(HttpServletRequest request, String code, String state) {
        if (TextUtil.isEmpty(state) || TextUtil.isEmpty(code)){
            return "绑定失败!";
        }
        if (!memberService.hasBindWeixin(state)) {
            WeixinInfo weixinInfo = wxPayService.getWeixinUserInfo(code);
            if (weixinInfo != null) {
                int i = memberService.bindWeixin(state, weixinInfo);
                if (i == -1) {
                    return "绑定失败!,一个微信号最多只能绑定一个咸鱼账号,有疑问请联系客服";
                }
                if (i == 1) {
                    asyncsComponent.sendNotice(state, "微信提现绑定成功", "微信提现绑定成功快去提现把!");
                    return "恭喜你,绑定成功!";
                } else {
                    return "绑定失败!!";
                }

            } else {
                return "绑定失败!";
            }
        }else{
            return "绑定失败!,账号已绑定微信";
        }
    }

    @PostMapping("/geturl")
    @ResponseBody
    public JsonData getUrl(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String url = wxPayService.getAuthUrl(requestHeaders.getUserid());
        return Callback.success("200", "查询成功", url);
    }

    @Login
    @PostMapping("/cancel")
    @ResponseBody
    public JsonData cancel(HttpServletRequest request, String memberId) {
        LbbMember member = memberService.selectMemberById(memberId);
        if (member == null) {
            return Callback.fail("200", "用户不存在");
        }
        member.setWxStatus(0);
        member.setWxOpenid("");
        memberService.updateMemberById(member);
        return Callback.success("200", "解绑成功");
    }

    @ResponseBody
    @PostMapping("/bind")
    public JsonData bind(HttpServletRequest request,String openid ,String nickname,Integer sex,String headimgurl) {
        if (TextUtil.isEmpty(openid)){
            return  Callback.fail("201","绑定失败!");
        }
        RequestHeaders requestHeaders = new RequestHeaders(request);
        WeixinInfo weixinInfo=new WeixinInfo();
        weixinInfo.setOpenId(openid);
        weixinInfo.setStatus(1);
        weixinInfo.setNickName(nickname);
        if (sex!=null && sex==1){
            weixinInfo.setGender( Gender.Male.name());
        }else {
            weixinInfo.setGender(Gender.Female.name());
        }
        weixinInfo.setAvatar(headimgurl);
        int i = memberService.bindWeixin(requestHeaders.getUserid(), weixinInfo);
        if (i == -1) {
            return Callback.fail("202","您已经绑定了微信,有疑问请联系客服");
        }
        if (i == 1) {
            asyncsComponent.sendNotice(requestHeaders.getUserid(), "微信提现绑定成功", "微信提现绑定成功快去提现把!");
            return  Callback.success("200", "恭喜您,绑定成功!");
        } else {
            return  Callback.fail("201","绑定失败!");
        }
    }

}
